using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
//using System.Web.UI.UserControl;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Url;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Roles;
using DotNetNuke.Entities.Modules.Communications;

namespace Jfc007.WebLib
{
    public partial class UserAndLoginControl : PortalModuleBase//, IActionable, IModuleListener
    {

#region "Constants"

#endregion

#region "Private Members"

        private string _userText;
        private string _loginText;
        private string _url;
        private string _cssClass;
        private string _logoffText;

#endregion

#region "Properties"

        public string UserText
        {
            get
            {
                return _userText;
            }
            set
            {
                _userText = value;
            }
        }

        public string LoginText
        {
            get
            {
                return _loginText;
            }
            set
            {
                _loginText = value;
            }
        }

        public string Url
        {
            get
            {
                return _url;
            }
            set
            {
                _url = value;
            }
        }

        public string CssClass
        {
            get
            {
                return _cssClass;
            }
            set
            {
                _cssClass = value;
            }
        }

        public string LogoffText
        {
            get
            {
                return _logoffText;
            }
            set
            {
                _logoffText = value;
            }
        }

#endregion

#region "Event Handlers"

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // public attributes
                if (!String.IsNullOrEmpty(CssClass))
                    cmdRegister.CssClass = CssClass;

                if (Request.IsAuthenticated == false)
                {
                    // User Control (Not Authenticated)
                    if (PortalSettings.UserRegistration != (int)Globals.PortalRegistrationType.NoRegistration)
                    {
                        if (!String.IsNullOrEmpty(UserText))
                        {
                            if (UserText.IndexOf("src=") != -1)
                                UserText = UserText.Replace("src=\"", "src=\"" + PortalSettings.ActiveTab.SkinPath);

                            cmdRegister.Text = UserText;
                        }
                        else
                        {
                            cmdRegister.Text = Localization.GetString("Register.Header", LocalResourceFile);
                        }

                        if (PortalSettings.Users < PortalSettings.UserQuota || PortalSettings.UserQuota == 0)
                        {
                            cmdRegister.Visible = true;
                        }
                        else
                        {
                            cmdRegister.Visible = false;
                        }
                    }
                    else
                    {
                        cmdRegister.Visible = false;
                    }

                    // Login Control (Not Authenticated)
                    if (!String.IsNullOrEmpty(LoginText))
                    {
                        if (LoginText.IndexOf("src=") != -1)
                            LoginText = LoginText.Replace("src=\"", "src=\"" + PortalSettings.ActiveTab.SkinPath);

                        cmdLogin.Text = LoginText;
                    }
                    else
                    {
                        cmdLogin.Text = Localization.GetString("Login.Header", LocalResourceFile);
                    }
                }
                else
                {
                    // User Control (Authenticated)
                    UserInfo objUserInfo = UserController.GetCurrentUserInfo();

                    if (objUserInfo.UserID != -1)
                    {
                        cmdRegister.Text = objUserInfo.DisplayName;
                        cmdRegister.ToolTip = Localization.GetString("ToolTip.Header", LocalResourceFile);
                    }

                    // Login Control (Authenticated)
                    if (!String.IsNullOrEmpty(LogoffText))
                    {
                        if (LogoffText.IndexOf("src=") != -1)
                            LogoffText = LogoffText.Replace("src=\"", "src=\"" + PortalSettings.ActiveTab.SkinPath);

                        cmdLogin.Text = LogoffText;
                    }
                    else
                        cmdLogin.Text = Localization.GetString("Logout.Header", LocalResourceFile);
                }
            }
            catch(Exception exc)    // Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cmdRegister_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.IsAuthenticated == false)
                {
                    if (PortalSettings.UserRegistration != (int)Globals.PortalRegistrationType.NoRegistration)
                    {
                        if (!String.IsNullOrEmpty(Url))
                            Response.Redirect(Url, true);
                        else
                        {
                            string ReturnUrl = HttpContext.Current.Request.RawUrl;

                            if (ReturnUrl.IndexOf("?returnurl=") != -1)
                                ReturnUrl = ReturnUrl.Substring(0, ReturnUrl.IndexOf("?returnurl="));

                            ReturnUrl = HttpUtility.UrlEncode(ReturnUrl);

                            if (PortalSettings.UserTabId != -1)
                            {
                                // user defined tab
                                Response.Redirect(Globals.NavigateURL(PortalSettings.UserTabId, "", "returnurl=" + ReturnUrl), true);
                            }
                            else
                            {
                                // portal tab
                                if (PortalSettings.HomeTabId != -1)
                                    Response.Redirect(Globals.NavigateURL(PortalSettings.HomeTabId, "Register", "returnurl=" + ReturnUrl), true);
                                else
                                    Response.Redirect(Globals.NavigateURL("Register", "returnurl=" + ReturnUrl), true);
                            }
                        }
                    }
                }
                else
                {
                    UserInfo objUserInfo = UserController.GetCurrentUserInfo();

                    if (objUserInfo.UserID != -1)
                    {
                        if (PortalSettings.UserTabId != -1)
                            Response.Redirect(Globals.NavigateURL(PortalSettings.UserTabId), true);
                        else
                            Response.Redirect(Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Profile", "UserID=" + objUserInfo.UserID.ToString()), true);
                    }
                }
            }
            catch (Exception exc) // Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.IsAuthenticated == true)
                {
                    if (HostSettings.GetHostSetting("UseFriendlyUrls") == "Y")
                        Response.Redirect(Globals.FriendlyUrl(PortalSettings.ActiveTab, Globals.ApplicationURL(PortalSettings.ActiveTab.TabID) + "&portalid=" + PortalSettings.PortalId.ToString(), "Logoff.aspx"), true);
                    else
                        Response.Redirect(ResolveUrl("~/Admin/Security/Logoff.aspx?tabid=" + PortalSettings.ActiveTab.TabID.ToString() + "&portalid=" + PortalSettings.PortalId.ToString()), true);
                }
                else
                {
                    string ReturnUrl = HttpContext.Current.Request.RawUrl;
                    if (ReturnUrl.IndexOf("?returnurl=") != -1)
                        ReturnUrl = ReturnUrl.Substring(0, ReturnUrl.IndexOf("?returnurl="));
                    
                    ReturnUrl = HttpUtility.UrlEncode(ReturnUrl);

                    if (PortalSettings.LoginTabId != -1 && Null.IsNull(Request.QueryString["override"]))
                    {
                        // user defined tab
                        Response.Redirect(Globals.NavigateURL(PortalSettings.LoginTabId, "", "returnurl=" + ReturnUrl), true);
                    }
                    else
                    {
                        // portal tab
                        if (PortalSettings.HomeTabId != -1)
                            Response.Redirect(Globals.NavigateURL(PortalSettings.HomeTabId, "Login", "returnurl=" + ReturnUrl), true);
                        else
                            Response.Redirect(Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Login", "returnurl=" + ReturnUrl), true);
                    }
                }
            }
            catch(Exception exc)    // Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

#endregion

#region " Web Form Designer Generated Code "

        /*
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
        */

#endregion

    }
}
